# name : step analysis by qunatile
# key : quantile.step.analysis
# contributor: Shuguang Sun
# --
proc stdize data=${1:dtin}
  PctlMtd=ORD_STAT
  outstat=${2:dtquant}
  pctlpts=20 to 80 by 10;
  var ${3:var};
run;

proc sql noprint;
  select _type_, $3
    into :pcent separated by ',', :pcentval separated by ","
    from $2 (where=(_type_ eq: "P"));
  quit;
run;
%put &=pcent.;
%put &=pcentval.;

proc print data=$2 noobs;
  where _type_ =: 'P';
run;

data ${4:dtout} (drop=_i _j);
  format ${5:grp} $20.;
  array pcts{7} _temporary_ (2:8);
  array quant_val{7} _temporary_;

  do _i = 1 to dim(pcts);
    quant_val[_i] = scan("&pcentval.", _i, ",");
    end;

  set $1;

  do _j = 1 to dim(pcts);
    if $3 le quant_val[_j] then do;
      $5 = "<=" || put(pcts[_j]*10, 2.) || "% (" || strip(put(quant_val[_j], best10.2)) || ")";
      output;
      end;
    else do;
      $5 = ">" || put(pcts[_j]*10, 2.) || "% (" || strip(put(quant_val[_j], best10.2)) || ")";
      output;
      end;
    end;
run;

proc sort data=$4;
  by ${6:usubjid};
run;
